/* Copyright (c) 2011, Ikaro Silva
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by the Framingham Heart Study and Sana Mobile.
4. Neither the name of Ikaro Silva, Sana Mobile, Framingham Heart Study, nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Ikaro Silva ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Ikaro Silva BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

YOU MAY CONTACT THE AUTHOR BY EMAIL (ikaro@mit.edu).
For updates to this software, please visit:
		http://code.google.com/p/framingham-heart-study/
 */
package org.framingham.risk;

/**
 * @author Ikaro Silva
 *
 */
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.util.Log;

//Implements the Coronary Heart Disease 10yr Risk assesment as decribed by
// http://www.framinghamheartstudy.org/risk/coronary.html

//Uses Maps to map values with the point system determined from the study


public class Chd {

	static Integer chd_risk= (Integer) 0;
	public static final String TAG = Chd.class.toString();
	
	public Integer get_risk(){
		return chd_risk ;
	}

	public String print_risk(){
		return "chd Risk: " + this.get_risk().toString()+ "%";
	}
	private static final Map<String, Integer> MAP_AGE_FEMALE = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" 45-49",-3);
				put(" 50-54",-2);
				put(" 55-59",0);
				put(" 60-64",1);
				put(" 65-69",3);
				put(" 70-74",4);
				put(" 75-79",6);
				put(" 80-84",7);
				put(" over/or 85",8);
			}});

	private static final Map<String, Integer> MAP_AGE_MALE = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" 45-49",1);
				put(" 50-54",2);
				put(" 55-59",3);
				put(" 60-64",4);
				put(" 65-69",5);
				put(" 70-74",6);
				put(" 75-79",7);
				put(" 80-84",7);
				put(" over 85",8);
			}});


	private static final Map<String, Integer> MAP_SBP = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" below 160",0);
				put(" over/or 160",1);
			}});

	private static final Map<String, Integer> MAP_HYPERTENSION = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" No",0);
				put(" Yes",1);
			}});

	private static final Map<String, Integer> MAP_BMI = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" below 30",0);
				put(" over/or 30",1);
			}});

	private static final Map<String, Integer> MAP_PRINTERVAL = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" below 160",0);
				put(" 160-199",1);
				put(" over/or 200",2);
			}});

	private static final Map<String, Integer> MAP_MURMURAGE = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" 45-54",5);
				put(" 55-64",4);
				put(" 65-74",2);
				put(" 75-84",1);
				put(" over/or 85",0);
			}});

	private static final Map<String, Integer> MAP_FAILURE = 
			Collections.unmodifiableMap(new HashMap<String, Integer>() {{ 
				put(" 45-54",10);
				put(" 55-64",6);
				put(" 65-74",2);
				put(" over/or 75-84",0);
			}});

	private static final Map<Integer, Integer> MAP_CHDRISK= 
			Collections.unmodifiableMap(new HashMap<Integer, Integer>() {{ 
				put(0,1);
				put(1,2);
				put(2,2);
				put(3,3);
				put(4,4);
				put(5,6);
				put(6,8);
				put(7,12);
				put(8,16);
				put(9,22);
				put(10,30);
			}});

	public void calcRisk(Map<String, String> data){

		Integer chd_pts=(Integer) 0;
		Iterator entries = data.entrySet().iterator();
		final String ans = new String();
		StringBuffer BP = new StringBuffer();
		String myGender=new String();
		while (entries.hasNext()) {
			Entry thisEntry = (Entry) entries.next();
			String MAP_PREDICTOR = (String) thisEntry.getKey();
			String PRED_VAL = (String) thisEntry.getValue();
			Log.i(TAG,"Mapping " + MAP_PREDICTOR +" to " + PRED_VAL);
			//MAP PREDICTOR SHOULD MATCH ELEMENT ID in the xml file!
			if(MAP_PREDICTOR.contentEquals("age")){
				if(myGender.contentEquals("male"))
					chd_pts =MAP_AGE_MALE.get(PRED_VAL.toString())+chd_pts;
				else if(myGender.contentEquals("female"))
					chd_pts =MAP_AGE_FEMALE.get(PRED_VAL.toString())+chd_pts;
				else
					Log.i("chd10yr","Incorrect gender");
			}
			else if(MAP_PREDICTOR.contentEquals("gender")){
				myGender =PRED_VAL.toString();
			}
			else if(MAP_PREDICTOR.contentEquals("sbp")){
				chd_pts =MAP_SBP.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("hypertension")){
				chd_pts =MAP_HYPERTENSION.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("bmi")){
				chd_pts =MAP_BMI.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("printerval")){
				chd_pts =MAP_PRINTERVAL.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("murmurage")){
				chd_pts =MAP_MURMURAGE.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("failure")){
				chd_pts =MAP_FAILURE.get(PRED_VAL.toString())+chd_pts;
			}
			else if(MAP_PREDICTOR.contentEquals("execute"))//do nothing
				continue;
		}

		//Set risk withing range
		if(chd_pts < 0)
			chd_pts=0;
		if(chd_pts>10)
			chd_pts=10;
		
		//Get Point for each conditions
		Log.i(TAG,"Calculating risk with " + chd_pts.toString());
		chd_risk =MAP_CHDRISK.get(chd_pts);
	}
	/*
	public static void main(String args[]) {
		// TODO Auto-generated method stub

		// Testing of getRisk
		Map<String, String> answers = 
				Collections.unmodifiableMap(new HashMap<String, String>() {{ 
					put("age","40-44");
					put("ldlc","100-159 [2.6-4.14]");
					put("chol","240-279 [6.22-7.24]");
					put("hdls","greater than 60 [greater than 1.56]");
					put("sbp","120-129");
					put("dbp","90-99");
					put("risk","smoker");
				}});

		Chd10yr myRisk= new Chd10yr();
		myRisk.getRisks(answers); //caculate risk
		//System.out.println("CHD LDL Risk is= " + myRisk.afib_risk  + " CHD Chol Risk is= " + myRisk.chol_risk);
	}
	 */
}
